package com.api.gbhg.lbj.action;

import com.api.gbhg.lbj.dao.ExcelDao;
import com.api.gbhg.lbj.tool.Tools;
import com.api.gbhg.lbj.util.ExcelBizHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.general.TimeUtil;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.soa.workflow.request.Cell;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.DetailTableInfo;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Row;

import java.util.List;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO 读取Excel 创建通用项目新增表
 * @date 2021/1/11 13:57
 */
public class TestExcelCronJob extends BaseCronJob {
    private static final Logger logger = LoggerFactory.getLogger(TestExcelCronJob.class);

    @Override
    public void execute() {
        logger.info("测试进入Excel创建流程-计划任务");
        RequestInfo requestinfo = new RequestInfo();
        requestinfo.setCreatorid("1");//创建者ID
        requestinfo.setWorkflowid("92"); // 工作流ID 92:通用项目表
        requestinfo.setRequestlevel("0"); // 紧急程度:0正常，1重要，2紧急
        requestinfo.setRemindtype("1"); // 提醒类型
        requestinfo.setIsNextFlow("1");//不自动流转至下一节点
        requestinfo.setDescription("lbj-excel测试"+ TimeUtil.getCurrentTimeString());//标题

        List<Object> xmmclist = Tools.getExcelData("C:\\Excel测试\\167057016000200001_海关仪器设备购置专项.xls","项目基本信息", "项目名称", 0);
        String xmmc = xmmclist.get(1).toString();//获取项目名称

        List<Object> sbdwlist = Tools.getExcelData("C:\\Excel测试\\167057016000200001_海关仪器设备购置专项.xls","项目基本信息", "项目单位", 0);
        String sbdw = sbdwlist.get(1).toString();//获取项目单位

        List<Object> ndlist = Tools.getExcelData("C:\\Excel测试\\167057016000200001_海关仪器设备购置专项.xls","项目基本信息", "计划开始执行年份", 0);
        String nd = ndlist.get(1).toString();//年度

        List<Object> dhlist = Tools.getExcelData("C:\\Excel测试\\167057016000200001_海关仪器设备购置专项.xls","项目基本信息", "联系人电话", 0);
        String lxdh = dhlist.get(1).toString();//联系人电话

        List<Object> fzrlist = Tools.getExcelData("C:\\Excel测试\\167057016000200001_海关仪器设备购置专项.xls","项目基本信息", "项目负责人", 0);
        String fzr = fzrlist.get(1).toString();//负责人
        logger.info("项目单位:"+sbdw+",负责人:"+fzr+",项目名称:"+xmmc+",年度:"+nd+"联系人电话:"+lxdh);
        MainTableInfo table = new MainTableInfo();
        table.addProperty(getPro("xmmc",xmmc+TimeUtil.getCurrentTimeString()));
        table.addProperty(getPro("sbdw", ExcelDao.getSBDWid(sbdw)));
        table.addProperty(getPro("sbks","169"));
        table.addProperty(getPro("sfdrgd","1"));//是否直接归档
        table.addProperty(getPro("ysnd",nd));
        table.addProperty(getPro("sfwdlhsdw","0"));
        table.addProperty(getPro("fzr",ExcelDao.getFZRid(fzr)));
        table.addProperty(getPro("jbr","1"));
        table.addProperty(getPro("lxdh",lxdh));
        requestinfo.setMainTableInfo(table);

        DetailTableInfo tables = new DetailTableInfo();
        DetailTable OneTable = new DetailTable();
        ExcelDao excelDao=new ExcelDao();
        List<ExcelBizHelper> exceData = excelDao.getExceData("C:\\Excel测试\\167057016000200001_海关仪器设备购置专项.xls");
        OneTable.setId("4");//表示明细表4
        for(int i=0;i<exceData.size();i++){//明细表行数
            Row row = new Row();
            String deptBudget = exceData.get(i).getDeptBudget();
            deptBudget=deptBudget.substring(deptBudget.indexOf("[")+1,deptBudget.indexOf("]"));
            String governmentBudget = exceData.get(i).getGovernmentBudget();
            governmentBudget=governmentBudget.substring(governmentBudget.indexOf("[")+1,governmentBudget.indexOf("]"));
            row.addCell(getCell("bmyszcjjfl",deptBudget));//部门预算支出经济分类
            row.addCell(getCell("hjwy",exceData.get(i).getTotal()));//合计（万元）
            row.addCell(getCell("dyzfjjfl",governmentBudget));//对应政府经济分类
            OneTable.addRow(row);
        }

        tables.addDetailTable(OneTable);
        requestinfo.setDetailTableInfo(tables);
        RequestService requestService  = new RequestService();
        String requestid = "";
        try {
            requestid = requestService.createRequest(requestinfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("TestCronJob流程创建成功="+requestid);
    }

    public Property getPro(String field,String value) {
        Property property = new Property();
        property.setName(field);
        property.setValue(value);
        return property;
    }

    public Cell getCell(String field,String value) {
        Cell cel = new Cell();
        cel.setName(field);
        cel.setValue(value);
        return cel;
     }
}

